/*-*
 * FILENAME  :
 *    $HeadURL$
 *
 * STATUS  :
 *    $Revision$
 *
 *    $Author$
 *    $Date$
 *  
 *    
 * Copyright (c) 2011 Gonalge Ltda. All rights reserved.
 *
 ****************************************************************/

package org.gonalge.sudoku.presentacion;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.Timer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import org.gonalge.sudoku.datos.Guardar;
import org.gonalge.sudoku.dto.Celda;
import org.gonalge.sudoku.negocio.Lista;
import org.gonalge.sudoku.negocio.Sudoku;
import org.gonalge.sudoku.util.FormUtils;

/**
 * 
 * @author Geremias Gonzalez
 */
public class FormSudoku extends JFrame implements KeyListener, MouseListener {

    /**
     * 
     */
    private static final long serialVersionUID = -3940847808134467079L;

    private Sudoku sudoku = new Sudoku();

    private final Guardar guardar = new Guardar();

    private final PanelAyuda ayuda = new PanelAyuda();

    private JTextField[][] tex = new JTextField[sudoku.getTabla().getFila()][sudoku.getTabla().getColumna()];

    private final JButton bb[][] = new JButton[3][5];

    private final JButton blanco = new JButton("Limpiar");

    private final JButton b16 = new JButton("16");

    private final JLabel labelVueltas = new JLabel("Nro Vueltas : 0");

    private final JMenuBar jMenuBar1 = new JMenuBar();

    private final JMenu jMenu1 = new JMenu();

    private final JMenuItem jMenuItem4 = new JMenuItem();

    private final JMenuItem jMenuItem5 = new JMenuItem();

    private final JMenuItem jMenuItem1 = new JMenuItem();

    private final JMenuItem jMenuItem2 = new JMenuItem();

    private final JMenu jMenu2 = new JMenu();

    private final JMenu jMenu3 = new JMenu();

    private final JButton probab = new JButton();

    private final JTextField fil = new JTextField();

    private final JTextField col = new JTextField();

    private final JLabel Texto = new JLabel();

    private final JMenu jMenu4 = new JMenu();

    private final JMenuItem jMenuItem8 = new JMenuItem();

    private final JMenuItem jMenuItem9 = new JMenuItem();

    private final JLabel fondo = new JLabel();

    private final JLabel Fila = new JLabel();

    private final JLabel Columna = new JLabel();

    private final JCheckBoxMenuItem jCheckBoxMenuItem1 = new JCheckBoxMenuItem();

    private final JCheckBoxMenuItem jCheckBoxMenuItem2 = new JCheckBoxMenuItem();

    private final JCheckBoxMenuItem jCheckBoxMenuItem3 = new JCheckBoxMenuItem();

    private final JMenuItem jMenuItem3 = new JMenuItem();

    private final JMenu jMenu5 = new JMenu();

    private final JMenuItem jMenuItem7 = new JMenuItem();

    private final JMenuItem jMenuItem10 = new JMenuItem();

    private final JMenuItem jMenuItem11 = new JMenuItem();

    private int x = 185, y = 70, fila = 0, columna = 0, alto = 60;

    private Font fon = new Font("Dialog", 2, 40);

    private final JButton sig = new JButton();

    private final JButton atra = new JButton();

    public JLabel tiempo = new JLabel();

    private final JButton nuevoButton = new JButton();

    private final JButton solucion = new JButton();

    private final FormUtils formUtils = new FormUtils();

    public FormSudoku() {
        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() throws Exception {

        this.getContentPane().setLayout(null);
        this.getContentPane().setBackground(Color.BLACK);

        this.setBounds(new Rectangle(0, 0, 1024, 770));
        formUtils.centerScreen(this);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setJMenuBar(jMenuBar1);
        this.setTitle("SUDOKU");
        this.setIconImage(formUtils.getImagen("sol2"));
        this.setResizable(false);

        labelVueltas.setBounds(700, 400, 200, 100);
        labelVueltas.setForeground(Color.GREEN);
        labelVueltas.setBounds(new Rectangle(830, 240, 130, 20));

        labelVueltas.setBackground(Color.blue);
        labelVueltas.setFont(new Font("Dialog", 1, 14));
        jMenu1.setText("Inicio");
        jMenuItem4.setText("Salir");
        jMenuItem4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                salir_actionPerformed(e);
            }
        });
        jMenuItem5.setText("Nuevo");
        jMenuItem5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                nuevo_actionPerformed(e);
            }
        });
        jMenuItem1.setText("Limpiar");
        jMenuItem1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                limpiar_actionPerformed(e);
            }
        });
        jMenuItem2.setText("Solucion");
        jMenuItem2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                solucion_actionPerformed(e);
            }
        });
        jMenu2.setText("Opciones");
        jMenu3.setText("Nivel");

        probab.setBounds(new Rectangle(850, 80, 140, 36));
        probab.setFont(new Font("Dialog", 1, 13));
        probab.setForeground(new Color(231, 0, 0));
        probab.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                probab_actionPerformed(e);
            }
        });
        probab.setIcon(new ImageIcon(formUtils.getImagen("prob2")));
        probab.setRolloverIcon(new ImageIcon(formUtils.getImagen("prob1")));

        probab.setBackground(new Color(0, 0, 165));
        probab.setBorder(BorderFactory.createLineBorder(Color.black, 1));

        probab.setOpaque(false);
        probab.setBorderPainted(false);
        fil.setBounds(new Rectangle(950, 140, 20, 20));
        fil.setText("0");

        col.setBounds(new Rectangle(950, 170, 20, 20));
        col.setText("0");

        Texto.setText("List { }");
        Texto.setBounds(new Rectangle(830, 200, 160, 25));
        Texto.setForeground(new Color(255, 33, 33));
        Texto.setFont(new Font("Dialog", 1, 14));

        jMenu4.setText("Help");
        jMenuItem8.setText("Guardar");
        jMenuItem8.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guardar_actionPerformed(e);
            }
        });
        jMenuItem9.setText("Recuperar");
        jMenuItem9.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                recuperar_actionPerformed(e);
            }
        });

        fondo.setIcon(new ImageIcon(formUtils.getImagen("fondo")));
        fondo.setBounds(new Rectangle(0, 0, fondo.getIcon().getIconWidth(), fondo.getIcon().getIconHeight()));

        Fila.setText("Fila");
        Fila.setBounds(new Rectangle(845, 140, 70, 20));
        Fila.setFont(new Font("Dialog", 0, 14));

        Columna.setText("Columna");
        Columna.setBounds(new Rectangle(845, 170, 75, 20));
        Columna.setFont(new Font("Dialog", 0, 14));

        jCheckBoxMenuItem1.setText("Facil");
        jCheckBoxMenuItem1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                facil_actionPerformed(e);
            }
        });
        jCheckBoxMenuItem2.setText("Medio");
        jCheckBoxMenuItem2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                medio_actionPerformed(e);
            }
        });
        jCheckBoxMenuItem3.setText("Dificil");
        jCheckBoxMenuItem3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dificil_actionPerformed(e);
            }
        });
        jMenuItem3.setText("Ayuda");
        jMenuItem3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ayuda_ActionPerformed(e);
            }
        });
        jMenu5.setText("Dimension");
        jMenuItem7.setText("4 X 4");
        jMenuItem7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dimension4_ActionPerformed(e);
            }
        });
        jMenuItem10.setText(" 9 X 9");
        jMenuItem10.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dimension9_actionPerformed(e);
            }
        });
        jMenuItem11.setText("16 X 16");
        jMenuItem11.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dimension16_actionPerformed(e);
            }
        });

        sig.setBounds(new Rectangle(850, 555, 140, 36));
        sig.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                siguiente_actionPerformed(e);
            }
        });
        sig.setIcon(new ImageIcon(formUtils.getImagen("sig2")));
        sig.setRolloverIcon(new ImageIcon(formUtils.getImagen("sig1")));

        sig.setOpaque(false);
        sig.setBorderPainted(false);
        atra.setIcon(new ImageIcon(formUtils.getImagen("atr2")));
        atra.setRolloverIcon(new ImageIcon(formUtils.getImagen("atr1")));
        atra.setBounds(new Rectangle(850, 595, 140, 36));
        atra.setOpaque(false);
        atra.setBorderPainted(false);
        atra.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                atraz_actionPerformed(e);
            }
        });

        tiempo.setText("Tiempo    00 : 00 : 00");
        tiempo.setBounds(new Rectangle(840, 0, 155, 30));
        tiempo.setBackground(Color.red);
        tiempo.setForeground(Color.red);
        tiempo.setFont(new Font("Dialog", 1, 15));

        nuevoButton.setIcon(new ImageIcon(formUtils.getImagen("nue1")));
        nuevoButton.setRolloverIcon(new ImageIcon(formUtils.getImagen("nue2")));
        nuevoButton.setBounds(new Rectangle(2, 155, nuevoButton.getIcon().getIconWidth(), nuevoButton.getIcon().getIconHeight()));
        nuevoButton.setBorderPainted(false);
        nuevoButton.addMouseListener(this);

        solucion.setIcon(new ImageIcon(formUtils.getImagen("sol1")));
        solucion.setRolloverIcon(new ImageIcon(formUtils.getImagen("sol2")));
        solucion.setBounds(new Rectangle(2, 215, solucion.getIcon().getIconWidth(), solucion.getIcon().getIconHeight()));
        solucion.setBorderPainted(false);
        solucion.addMouseListener(this);

        crearTextField();
        crearBotones();
        timer.start();

        jCheckBoxMenuItem1.setSelected(false);
        jCheckBoxMenuItem2.setSelected(true);
        jCheckBoxMenuItem3.setSelected(false);

        jMenu1.add(jMenuItem5);
        jMenu1.add(jMenuItem1);
        jMenu1.add(jMenuItem2);
        jMenu1.add(jMenuItem8);
        jMenu1.add(jMenuItem9);
        jMenu1.addSeparator();
        jMenu1.add(jMenuItem4);
        jMenuBar1.add(jMenu1);
        jMenu3.add(jCheckBoxMenuItem1);
        jMenu3.add(jCheckBoxMenuItem2);
        jMenu3.add(jCheckBoxMenuItem3);
        jMenu2.add(jMenu3);
        jMenu5.add(jMenuItem7);
        jMenu5.add(jMenuItem10);
        jMenu5.add(jMenuItem11);
        jMenu2.add(jMenu5);
        jMenuBar1.add(jMenu2);
        jMenu4.add(jMenuItem3);
        jMenuBar1.add(jMenu4);

        this.getContentPane().add(solucion, null);
        this.getContentPane().add(nuevoButton, null);
        this.getContentPane().add(tiempo, null);
        this.getContentPane().add(atra, null);
        this.getContentPane().add(sig, null);
        this.getContentPane().add(ayuda, null);
        this.getContentPane().add(Columna, null);
        this.getContentPane().add(Fila, null);
        this.getContentPane().add(Texto, null);
        this.getContentPane().add(col, null);
        this.getContentPane().add(fil, null);
        this.getContentPane().add(probab, null);
        this.getContentPane().add(labelVueltas, null);
        this.getContentPane().add(fondo, null);

        ayuda.setVisible(false);

    }

    public void cargarMatriz() {
        for (byte i = 0; i < sudoku.getTabla().getFila(); i++)
            for (byte j = 0; j < sudoku.getTabla().getColumna(); j++) {

                if (tex[i][j].getText().compareTo("") != 0)
                    sudoku.getTabla().insertar(i, j, Integer.parseInt(tex[i][j].getText()));
                else
                    sudoku.getTabla().insertar(i, j, 0);
            }
        sudoku.setNroVueltas(0);
    }

    public void llenarText() {
        for (byte i = 0; i < sudoku.getTabla().getFila(); i++)
            for (byte j = 0; j < sudoku.getTabla().getColumna(); j++) {
                if (sudoku.getTabla().Consultar(i, j) != 0)
                    tex[i][j].setText("" + sudoku.getTabla().Consultar(i, j));
                else
                    tex[i][j].setText("");
            }
    }

    private void crearBotones() {
        for (int i = 0; i < 5; i++)
            for (int j = 0; j < 3; j++) {
                bb[j][i] = new JButton("" + (i * 3 + j + 1));
                bb[j][i].setBounds(825 + j * 61, 300 + i * 40, 61, 40);
                bb[j][i].setBackground(Color.ORANGE);
                bb[j][i].addMouseListener(this);
                this.getContentPane().add(bb[j][i]);
            }

        b16.addMouseListener(this);
        b16.setBounds(825, 500, 61, 40);
        b16.setBackground(Color.ORANGE);
        this.getContentPane().add(b16);

        blanco.addMouseListener(this);
        blanco.setBounds(885, 500, 123, 40);
        blanco.setBackground(Color.ORANGE);
        this.getContentPane().add(blanco);

    }

    private void colorText() {
        Color co1 = Color.blue;
        Color co2 = new Color(198, 148, 0);

        boolean sw = true;

        int n = sudoku.getTabla().getDimCuadrante();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int i1 = i * n; i1 < n + i * n; i1++)
                    for (int j1 = j * n; j1 < n + j * n; j1++) {
                        if (sw)
                            tex[i1][j1].setBackground(co1);
                        else
                            tex[i1][j1].setBackground(co2);
                    }

                sw = !sw;
            }
            if (n % 2 == 0)
                sw = !sw;
        }

    }

    private void crearTextField() {

        int n = sudoku.getTabla().getColumna();
        tex = new JTextField[n][n];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) {
                tex[i][j] = new JTextField("");
                tex[i][j].setFont(fon);
                tex[i][j].setHorizontalAlignment(JTextField.CENTER);
                tex[i][j].setForeground(Color.BLACK);
                tex[i][j].addMouseListener(this);
                tex[i][j].addKeyListener(this);
                tex[i][j].setBounds(x + j * alto, y + i * alto, alto, alto);
                this.getContentPane().add(tex[i][j]);
            }
        colorText();
        this.getContentPane().add(fondo, null);
    }

    public void actualizarTx() {

        for (byte i = 0; i < sudoku.getTabla().getFila(); i++)
            for (byte j = 0; j < sudoku.getTabla().getColumna(); j++)
                tex[i][j].setEnabled(true);

        for (byte i = 0; i < sudoku.getAleatoCellS().getSize(); i++) {
            Celda c = (Celda) sudoku.getAleatoCellS().getEle(i);
            tex[c.getFila()][c.getColumna()].setEnabled(false);
        }

    }

    private void nuevo_actionPerformed(ActionEvent e) {
        hh = mm = ss = 0;
        int ni = sudoku.getNivel();
        nuevoJuego(sudoku.getTabla().getColumna());
        sudoku.setNivel(ni);
        colorText();
        sudoku.llenadoAleatorio();
        llenarText();
        actualizarTx();
        Texto.setText("List { }");
        labelVueltas.setText("Nro Vueltas : 0");
        ayuda.setVisible(false);
    }

    private void limpiar_actionPerformed(ActionEvent e) {

        sudoku.vaciarTablero();
        llenarText();

        for (int i = 0; i < sudoku.getTabla().getFila(); i++)
            for (int j = 0; j < sudoku.getTabla().getColumna(); j++)
                tex[i][j].setEnabled(true);
        labelVueltas.setText("Nro Vueltas : " + sudoku.getNroVueltas());

    }

    private void salir_actionPerformed(ActionEvent e) {
        System.exit(EXIT_ON_CLOSE);
    }

    private void solucion_actionPerformed(ActionEvent e) {
        colorText();
        sudoku.vaciarTablero();
        cargarMatriz();
        if (sudoku.BacktrackSolucion()) {
            llenarText();
            labelVueltas.setText("Nro Vueltas : " + sudoku.getNroVueltas());
        } else
            labelVueltas.setText("No tiene Solucion");

    }

    // ---Evento--de--teclado-----
    @Override
    public void keyReleased(KeyEvent e) {
        llenarText();
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {

        fila = 0;
        columna = 0;
        for (int i = 0; i < sudoku.getTabla().getFila(); i++)
            for (int j = 0; j < sudoku.getTabla().getColumna(); j++) {
                if (tex[i][j] == e.getComponent()) {
                    fila = i;
                    columna = j;
                    i = sudoku.getTabla().getFila() + 1;
                    j = sudoku.getTabla().getColumna() + 1;
                }
            }

        if (e.getKeyCode() >= 97 && e.getKeyCode() <= 105 && sudoku.getTabla().Consultar(fila, columna) == 0) {
            sudoku.getTabla().insertar(fila, columna, Integer.parseInt("" + e.getKeyChar()));
            sudoku.getAux().insertar(fila, columna, Integer.parseInt("" + e.getKeyChar()));
        }

    }

    // ---Evento--del--Mouse---------
    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        llenarText();
    }

    @Override
    public void mousePressed(MouseEvent e) {

        if (e.getComponent() instanceof JTextField) {

            fila = 0;
            columna = 0;
            for (int i = 0; i < sudoku.getTabla().getFila(); i++)
                for (int j = 0; j < sudoku.getTabla().getColumna(); j++) {
                    if (tex[i][j] == e.getComponent()) {
                        fila = i;
                        columna = j;
                        i = j = sudoku.getTabla().getFila() + 1;
                    }
                }
        } else {
            colorText();
            if (blanco != e.getComponent()) {
                int a = 0;
                for (int i = 0; i < 5; i++)
                    for (int j = 0; j < 3; j++) {
                        if (bb[j][i] == e.getComponent()) {
                            a = Integer.parseInt(bb[j][i].getText());
                            i = j = 6;

                        }
                    }

                if (b16 == e.getComponent())
                    a = 16;
                if (sudoku.getTabla().Consultar(fila, columna) == 0) {
                    sudoku.getTabla().insertar(fila, columna, a);
                    sudoku.getAux().insertar(fila, columna, a);
                }
                for (int i = 0; i < sudoku.getTabla().getFila(); i++)
                    for (int j = 0; j < sudoku.getTabla().getColumna(); j++) {
                        if (sudoku.getTabla().Consultar(i, j) == a)
                            tex[i][j].setBackground(Color.YELLOW);
                    }

            }
            if (nuevoButton == e.getSource()) {
                hh = mm = ss = 0;
                int ni = sudoku.getNivel();
                nuevoJuego(sudoku.getTabla().getColumna());
                sudoku.setNivel(ni);
                colorText();
                sudoku.llenadoAleatorio();
                llenarText();
                actualizarTx();
                Texto.setText("List { }");
                labelVueltas.setText("Nro Vueltas : 0");
                ayuda.setVisible(false);
            }

            if (solucion == e.getSource()) {
                colorText();
                sudoku.vaciarTablero();
                cargarMatriz();
                if (sudoku.BacktrackSolucion()) {
                    llenarText();
                    labelVueltas.setText("Nro Vueltas : " + sudoku.getNroVueltas());
                } else
                    labelVueltas.setText("No tiene Solucion");

            }

        }
    }

    // -------------------------------
    private void probab_actionPerformed(ActionEvent e) {

        int i, j;
        i = Integer.parseInt(fil.getText());
        j = Integer.parseInt(col.getText());
        Lista l = sudoku.Posibilidades(i, j);
        String s = "";
        while (!l.isEmpty()) {
            Celda c = (Celda) l.getEleNext();
            s = c.getEle() + "," + s;
        }
        if (s.length() != 0)
            Texto.setText("List{" + s.substring(0, s.length() - 1) + "}");
        else
            Texto.setText("List { }");
    }

    private void guardar_actionPerformed(ActionEvent e) {
        guardar.guardar(sudoku);
    }

    private void recuperar_actionPerformed(ActionEvent e) {
        colorText();
        Sudoku aa = sudoku;
        try {
            int nn = sudoku.getTabla().getFila();

            sudoku = guardar.recuperar();

            for (int i = 0; i < nn; i++)
                for (int j = 0; j < nn; j++)
                    remove(tex[i][j]);

            repaint();

            switch (sudoku.getTabla().getColumna()) {
                case 4: {
                    x = 300;
                    y = 180;
                    alto = 80;
                    fon = new Font("Dialog", 2, 60);
                }
                    break;
                case 9: {
                    x = 185;
                    y = 70;
                    alto = 60;
                    fon = new Font("Dialog", 2, 40);
                }
                    break;
                case 16: {
                    x = 150;
                    y = 45;
                    alto = 39;
                    fon = new Font("Dialog", 2, 20);
                }

            }

            crearTextField();
            llenarText();
            actualizarTx();
            Texto.setText("List { }");
            labelVueltas.setText("Nro Vueltas : " + sudoku.getNroVueltas());

            switch (sudoku.getNivel()) {
                case 15: {
                    jCheckBoxMenuItem1.setSelected(false);
                    jCheckBoxMenuItem2.setSelected(false);
                    jCheckBoxMenuItem3.setSelected(true);
                }
                    ;
                    break;
                case 35: {
                    jCheckBoxMenuItem1.setSelected(false);
                    jCheckBoxMenuItem2.setSelected(true);
                    jCheckBoxMenuItem3.setSelected(false);
                }
                    ;
                    break;
                case 60: {
                    jCheckBoxMenuItem1.setSelected(true);
                    jCheckBoxMenuItem2.setSelected(false);
                    jCheckBoxMenuItem3.setSelected(false);
                }
                    ;
                    break;

            }
        } catch (Exception e1) {
            sudoku = aa;
            JOptionPane.showMessageDialog(this, "No hay ningun juego Guardado", "BRUTO...!", 2);
        }
    }

    private void facil_actionPerformed(ActionEvent e) {

        sudoku.setNivel(6 * sudoku.getTabla().getDimCuadrante());
        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem2.setSelected(false);
        jCheckBoxMenuItem3.setSelected(false);
    }

    private void medio_actionPerformed(ActionEvent e) {
        sudoku.setNivel((6 * sudoku.getTabla().getDimCuadrante() - sudoku.getTabla().getDimCuadrante()) / 2);
        jCheckBoxMenuItem1.setSelected(false);
        jCheckBoxMenuItem2.setSelected(true);
        jCheckBoxMenuItem3.setSelected(false);
    }

    private void dificil_actionPerformed(ActionEvent e) {

        sudoku.setNivel(sudoku.getTabla().getDimCuadrante());
        jCheckBoxMenuItem1.setSelected(false);
        jCheckBoxMenuItem2.setSelected(false);
        jCheckBoxMenuItem3.setSelected(true);
    }

    private void ayuda_ActionPerformed(ActionEvent e) {
        ayuda.empesar();
        ayuda.setVisible(true);

    }

    private void nuevoJuego(int n) {
        repaint();
        for (int i = 0; i < sudoku.getTabla().getFila(); i++)
            for (int j = 0; j < sudoku.getTabla().getColumna(); j++)
                remove(tex[i][j]);
        sudoku = new Sudoku(n);
        tex = new JTextField[n][n];
        crearTextField();
    }

    private void dimension4_ActionPerformed(ActionEvent e) {
        x = 300;
        y = 180;
        alto = 80;
        fon = new Font("Dialog", 2, 60);
        nuevoJuego(4);
    }

    private void dimension9_actionPerformed(ActionEvent e) {
        x = 185;
        y = 70;
        alto = 60;
        fon = new Font("Dialog", 2, 40);
        nuevoJuego(9);
    }

    private void dimension16_actionPerformed(ActionEvent e) {
        x = 150;
        y = 42;
        alto = 38;
        fon = new Font("Dialog", 2, 25);
        nuevoJuego(16);
    }

    private void nuevaSolucion() {
        if (sudoku.BacktrackSolucion()) {
            llenarText();
            labelVueltas.setText("Nro Vueltas : " + sudoku.getNroVueltas());
        } else
            labelVueltas.setText("No tiene Solucion");
        Texto.setText("List { }");

    }

    private void siguiente_actionPerformed(ActionEvent e) {
        sudoku.setTabla(sudoku.getAux());
        sudoku.acualizarFilColEleSol(true);
        nuevaSolucion();

    }

    private void atraz_actionPerformed(ActionEvent e) {
        sudoku.setTabla(sudoku.getAux());
        sudoku.acualizarFilColEleSol(false);
        nuevaSolucion();
    }

    public static void main(String[] args) {
        new FormSudoku().setVisible(true);
    }

    // ********TIEMPO*********
    private int hh = 0, mm = 0, ss = 0;

    private String h = "00", m = "00", s = "00";

    public Timer timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            s = (ss < 10) ? "0" + ss : ss + "";
            m = (mm < 10) ? "0" + mm : mm + "";
            h = (hh < 10) ? "0" + hh : hh + "";

            if (ss < 59) {
                ss++;
                tiempo.setText("Tiempo    " + h + " : " + m + " : " + s);
            } else {
                if (mm < 59) {
                    mm++;
                    tiempo.setText("Tiempo    " + h + " : " + m + " : " + s);
                } else {
                    mm = 0;
                    hh++;
                    tiempo.setText("Tiempo    " + h + " : " + m + " : " + s);
                }
                ss = 0;
            }
        }
    });
}
